// 'use strict'
const path = require('path')
// const port = 9529 // dev port

function resolve(dir) {
  return path.join(__dirname, dir)
}

// 开发模式代理地址 TODO: 按需修改
// const DEV_URL = 'https://ibox.sdxgxx.cn/'
const DEV_URL = 'https://ibox.tommmy.top/'


module.exports = {
  lintOnSave: false,
  publicPath: './',
  devServer: {
    host: 'box.tommmy.top',
    port: 80,
    proxy: {
      '^/api': {
        target: DEV_URL,
        changeOrigin: false,
        pathRewrite: {
          '^/api': '',
        },
      },
    },
  },
  css:{
    extract: true
  },
  configureWebpack: {
    name: '',
    resolve: {
      alias: {
        '@': resolve('src'),
      },
    },
    devtool: 'source-map',
  },
  chainWebpack: (config) => {
    config.plugins.delete('preload') // TODO: need test
    config.plugins.delete('prefetch') // TODO: need test

    config.module

      .rule('less')

      .oneOf('vue')

      .use('px2rem-loader')

      .loader('px2rem-loader')

      .before('postcss-loader') // this makes it work.

      .options({ remUnit: 75, remPrecision: 8 }) // remUnit: 192代表以1920px为整体，如果设计稿的尺寸是750px，这里的值为75

      .end()

    // set preserveWhitespace
    config.module
      .rule('vue')
      .use('vue-loader')
      .loader('vue-loader')
      .tap((options) => {
        options.compilerOptions.preserveWhitespace = true
        return options
      })
      .end()

    config
      // https://webpack.js.org/configuration/devtool/#development
      .when(process.env.NODE_ENV === 'dev', (config) =>
        config.devtool('cheap-source-map')
      )

    config.when(process.env.NODE_ENV !== 'dev', (config) => {
      config
        .plugin('ScriptExtHtmlWebpackPlugin')
        .after('html')
        .use('script-ext-html-webpack-plugin', [
          {
            // `runtime` must same as runtimeChunk name. default is `runtime`
            inline: /runtime\..*\.js$/,
          },
        ])
        .end()
      config.optimization.splitChunks({
        chunks: 'all',
        cacheGroups: {
          libs: {
            name: 'chunk-libs',
            test: /[\\/]node_modules[\\/]/,
            priority: 10,
            chunks: 'initial', // only package third parties that are initially dependent
          },
          commons: {
            name: 'chunk-commons',
            test: resolve('src/components'), // can customize your rules
            minChunks: 3, //  minimum common number
            priority: 5,
            reuseExistingChunk: true,
          },
        },
      })
      config.optimization.runtimeChunk('single')
    })
  },
}
